/*
 * @Date: 2023-01-10 13:28:06
 * @LastEditTime: 2023-01-31 13:59:37
 * @FilePath: /wkw/src/hooks/routeCache.ts
 * 介绍:路由缓存hooks
 */
import { ref, nextTick } from "vue";

export const caches = ref<string[]>([]);

// 添加缓存的路由组件
export function addCache(componentName: string | string[]) {
  if (Array.isArray(componentName)) {
    componentName.forEach(addCache);
    return;
  }
  if (!componentName || caches.value.includes(componentName)) return;
  caches.value.push(componentName);
}

// 移除缓存的路由组件
export function removeCache(componentName: string) {
  const index = caches.value.indexOf(componentName);
  if (index > -1) {
    return caches.value.splice(index, 1);
  }
}

// 移除缓存的路由组件的实例
export async function removeCacheEntry(componentName: string) {
  if (removeCache(componentName)) {
    await nextTick();
    addCache(componentName);
  }
}
